package com.ld.security.common.core.modal.chain;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 代理链管理器
 * @param <E>
 */
public class ChainManager<E extends ChainItem> {

    @Autowired(required = false)
    private List<E> items;

    /**
     * 允许多个节点处理
     */
    private boolean chainMany=false;


    public ChainManager() {

    }

    public ChainManager(boolean chainMany) {
        this.chainMany = chainMany;
    }

    /**
     * 处理任务
     * @param val
     * @return
     */
    public Object apply(Object val,String oper){
        if(items != null && val != null){
            for (E item : items) {
                if(item.match(val)){
                    val = item.apply(val,oper);
                    if(!chainMany){
                        break;
                    }
                }
            }
        }
        return val;
    }

    public Object apply(Object val){
        return apply(val,null);
    }

        public List<E> getItems() {
        return items;
    }

    public void setItems(List<E> items) {
        this.items = items;
    }
}
